package com.liuxing.javaSpringBoot.modules.account.dao;

import com.liuxing.javaSpringBoot.modules.account.entity.User;
import com.liuxing.javaSpringBoot.modules.common.entity.SearchBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * @File UserDao.java
 * @Auth liuxing
 * @Date 2021/8/22 10:36
 * @Email liuxing997@foxmail.com
 */
@Mapper
@Repository
public interface UserDao {

    //添加用户信息
    @Insert("insert into account_user (email, user_name, password, user_image, create_date) "
            + "values (#{email}, #{userName}, #{password}, #{userImage}, #{createDate})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insertUser(User user);

    //根据用户名查询用户信息
    @Select("select * from account_user where user_name=#{userName}")
    User getUserByUsername(@Param("userName") String userName);

    //根据用户名或邮箱查询用户信息
    @Select("select * from account_user where email = #{email} or user_name = #{userName}")
    List<User> getUserByUserNameOrEmail(@Param("email") String email, @Param("userName") String userName);

    //根据用户ID修改用户信息
    @Update("update account_user set user_name = #{userName}, email = #{email}, password = #{password},user_image = #{userImage}" +
            "where id = #{id}")
    void updateUserById(User user);

    //根据用户ID删除用户
    @Delete("delete from account_user where id = #{id}")
    void deleteUserByUserId(int id);

    //根据用户ID查询用户
    @Select("select * from account_user where id = #{id}")
    @Results(id="userResult", value={
            @Result(column="id", property="id"),
            @Result(column="id",property="roles",
                    javaType=List.class,
                    many=@Many(select="com.liuxing.javaSpringBoot.modules.account.dao.RoleDao.getRolesByUserId"))
    })
    User getUserByUserId(@Param("id") int id);

    //查询所有用户信息
    @Select("select * from account_user")
    @Results(id="usersResult", value={
            @Result(column="id", property="id"),
            @Result(column="id",property="roles",
                    javaType=List.class,
                    many=@Many(select="com.liuxing.javaSpringBoot.modules.account.dao.RoleDao.getRolesByUserId"))
    })
    List<User> getAllUser();

    //批量插入用户信息
    @Insert("<script>"
            + "insert into account_user (create_date,user_name, email, password)" +
            " values <foreach collection='users' item='user' index='index' separator=','>"
            + "(#{user.createDate},#{user.name}, #{user.email}, #{user.password})</foreach>"
            + "</script>")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void insertUsers(@Param("users") List<User> users);

    //分页查询用户信息
    @Select("<script>"
            + "select * from account_user "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (user_name like '%${keyword}%' or "
            + " email like '%${keyword}%') "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
   List<User> getUsersBySearchBean(SearchBean searchBean);
}
